#!/bin/env python
# -*- coding: utf-8  -*-
import csv
import chardet
import urllib2
import socket
import bs4
import codecs

TIME_OUT=10

url = 'http://liansai.500.com/zuqiu-%s/'

def parserank(urlstring,league):
	try:
		print urlstring
		html = urllib2.urlopen(urlstring,timeout=TIME_OUT).read()
		soup = bs4.BeautifulSoup(html,from_encoding="gb18030")
		divsoup = soup.find('div','lbox')
		tablesoup = divsoup.find('table','lstable1 ljifen_top_list_s jTrHover')
		if(tablesoup):
			line = tablesoup.findAll('tr')
			for num in range(1,len(line)):
				tdsoup = line[num].findAll('td')
				print tdsoup[0].string
				print tdsoup[1].string
				if tdsoup[0].string is None or tdsoup[1].string is None:
					return -2
				fh = codecs.open('teamrank.csv','a','gbk')
				fh.write(tdsoup[1].string+','+tdsoup[0].string+','+league + '\n')
				fh.close()
		else:
			print 'no attribute find'
			
	except urllib2.HTTPError,e:
		print e.code
		return -1
	except urllib2.URLError,e:
		return -1
	except socket.timeout, e: 
		return -1
	except NameError:
		print 'var not defined'
		return -2



if __name__ == '__main__':
	urlstring = url % 3015
	html = urllib2.urlopen(urlstring,timeout=TIME_OUT).read()
	soup = bs4.BeautifulSoup(html,from_encoding="gb18030")
	ranksoup = soup.find('select','lpage_race_head_sel1 lselect')
	ranktable = ranksoup.findAll('option')
	for num in range(1,len(ranktable)):
		print 'num' + str(num)
		urlstring =  url % ranktable[num]['value']
		ret = parserank(urlstring,ranktable[num].string)
		if(ret==-1):
			num-=1
			continue

		


